<template>
  <div class="login">
    <div>
      <van-form label-align="center">
        <van-field
          v-model="loginForm.username"
          name="账号"
          label="账号"
          placeholder="test"
          :rules="[{ required: true, message: '请填写账号test' }]"
        />
        <van-field
          v-model="loginForm.password"
          type="password"
          name="密码"
          label="密码"
          placeholder="123456"
          :rules="[{ required: true, message: '请填写密码123456' }]"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            @click="login()"
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
    <div class="link-area">
      <a @click="register">还没有账号？前往注册</a>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
    };
  },
  computed: {
    ...mapMutations("user", ["setUser"]),
    ...mapGetters("user", ["isLogin"]),
  },
  methods: {
    login() {
      if (this.loginForm.username != "" && this.loginForm.password != "") {
        this.$store.commit("user/login", [
          this.loginForm.username,
          this.loginForm.password,
        ]);
        if (this.isLogin) {
          this.$toast.success("登录成功");
          this.$router.replace({ name: "user" });
        } else {
          this.$notify("账户名或密码错误");
        }
      }
    },
    register() {
      this.$router.push({ name: "register" });
    },
  },
};
</script>
<style scoped>
.link-area {
  display: block;
  margin-top: 25px;
  text-align: center;
}
a {
  color: #1989fa;
}
</style>
